import numpy as np


def rotate(ordinate, basic, angle=None):
    if angle is None:  # 默认旋转到y=0
        angle = -np.arctan(basic[1] / basic[0])
    else:
        angle = -(np.arctan(basic[1] / basic[0]) - angle)
    rotate_ordinate = np.zeros(ordinate.shape)
    rotate_ordinate[:, 0] = ordinate[:, 0] * np.cos(angle) - ordinate[:, 1] * np.sin(angle)
    rotate_ordinate[:, 1] = ordinate[:, 0] * np.sin(angle) + ordinate[:, 1] * np.cos(angle)

    return rotate_ordinate


def rotate_dot(ordinate, angle, point):
    rotate_ordinate = np.zeros(ordinate.shape)
    rotate_ordinate[:, 0] = (ordinate[:, 0] - point[0]) * np.cos(angle) - (ordinate[:, 1] - point[1]) * np.sin(angle) + \
                            point[0]
    rotate_ordinate[:, 1] = (ordinate[:, 0] - point[0]) * np.sin(angle) + (ordinate[:, 1] - point[1]) * np.cos(angle) + \
                            point[1]

    return rotate_ordinate


def move_x(ordinate, move_dis):
    ordinate[:, 0] += move_dis

    return ordinate


def move_y(ordinate, move_dis):
    ordinate[:, 1] += move_dis

    return ordinate


def set0(ordinates):
    ordinates = np.asarray(ordinates)
    len = ordinates.shape[0]
    for i in [0, int(len / 2), len - 1]:
        if abs(ordinates[i, 0]) < 1e-10:
            ordinates[i, 0] = 0
        if abs(ordinates[i, 1]) < 1e-10:
            ordinates[i, 1] = 0
        if abs(ordinates[i, 0] - 1) < 1e-10:
            ordinates[i, 0] = 1

    return ordinates
